package com.xiaoke.api.controller;
import com.google.common.collect.Maps;
import com.xiaoke.api.service.*;
import com.xiaoke.entity.*;
import com.xiaoke.util.common.AjaxMsg;
import com.xiaoke.util.common.ValidUtil;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/index")
public class IndexController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private ChannelService ChannelService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public AjaxMsg index(HttpServletRequest request){
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);
        if(ValidUtil.isEmpty(shop))
            return new AjaxMsg(AjaxMsg.FAILURE,"店铺不存在！");

        List<Banner> banner = bannerService.findByShop(shop.getId());
        List<Category> channel = categoryService.findByParentId(0);

        Map<String,Object> params = Maps.newHashMap();
        params.put("shopId",shop.getId());
        List<Goods> newGoodsList = goodsService.findByParams(params, 1, 6);
        params.put("sellVolume",1);
        List<Goods> hotGoodsList = goodsService.findByParams(params, 1, 6);
        params.remove("sellVolume");
        params.put("isHot",1);
        List<Goods> recommendGoodsList = goodsService.findByParams(params, 1, 6);

        Map<String,Object> result = Maps.newHashMap();
        result.put("banner",banner);
        result.put("channel",channel);
        result.put("newGoodsList",newGoodsList);
        result.put("hotGoodsList",hotGoodsList);
        result.put("recommendGoodsList",recommendGoodsList);
        return new AjaxMsg(result);
    }
}
